import dayjs from "dayjs";

// 使对象变为仅可读， 严格模式下，修改报错
export const createEnum = <T extends {[key: string]: string | number}>(data: T): Readonly<T> => {
    return Object.freeze(data);
};

interface UploadFileParams {
    file?: "file";
    accept?: string;
    onChange: (fileList: File[]) => void;
}

/**
 * 上传文件函数
 *
 * @param file 文件类型，默认为 "file"
 * @param accept 可接受的文件类型
 * @param onChange 文件改变时的回调函数
 * @returns 无返回值
 */
export const uploadFile = ({file, accept, onChange}: UploadFileParams) => {
    const input = document.createElement("input");
    input.type = file || "file";
    input.style.display = "none";
    input.accept = accept;
    input.addEventListener("change", async (e: any) => {
        const files = e.target.files;
        await onChange(files);
        input.remove();
    });
    input.click();
};

// 判断是否为开发环境
export const isDev = process.env.NODE_ENV === "development";

/**
 * 解析时间
 * @param time 时间
 */

export const parseTime = (time: string) => {
    const format = "YYYY-MM-DD HH:mm:ss";
    return dayjs(time).format(format);
};
